package com.jdjw.modules.abuttingjoint.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.abuttingjoint.entity.DwdGcFkxxJiande;
import com.jdjw.modules.abuttingjoint.mapper.DwdGcFkxxJiandeMapper;
import com.jdjw.modules.abuttingjoint.service.DwdGcFkxxJiandeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.abuttingjoint.timing.TimedTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工程-付款信息 服务实现类
 * </p>
 *
 * @author guxinao
 * @since 2021-07-02
 */
@Service
@Slf4j
@DS("sjwjcjdcz")
@Component
public class DwdGcFkxxJiandeServiceImpl extends ServiceImpl<DwdGcFkxxJiandeMapper, DwdGcFkxxJiande> implements DwdGcFkxxJiandeService {

    private static DwdGcFkxxJiandeMapper dwdGcFkxxJiandeMapper;

    @Resource
    public void setDwdGcFkxxJiandeMapper(DwdGcFkxxJiandeMapper dwdGcFkxxJiandeMapper) {
        DwdGcFkxxJiandeServiceImpl.dwdGcFkxxJiandeMapper = dwdGcFkxxJiandeMapper;
    }


    @Override
    public ResponseResult doPostTestTwoq(String time) {
        ResponseResult results = new ResponseResult();
        results.setMsg("测试关闭");
        return results;
    }

    public static void doPostTestTwoq(String time,String dateDq) {
        String url = "http://39.170.77.212:8096/sysn/data/gc-fkxx";
        ArrayList list = new ArrayList();
        ResponseResult results = new ResponseResult();

        try{
            //查询工程-付款信息人员
            QueryWrapper entityWrapper = new QueryWrapper();
            entityWrapper.like("uploadtime", time);
            List<DwdGcFkxxJiande> all = dwdGcFkxxJiandeMapper.selectList(entityWrapper);

            if(all == null || all.size()<1){
                log.info("工程-付款信息：数据库查询数据为0条");
                return;
            }

            for(DwdGcFkxxJiande a:all){
                if(a.getCwh()!=null && a.getXh()!=null){
                    //将数据的上传时间改为推送时间
                    a.setUploadtime(dateDq);
                    //转化为json字符串
                    String jsonStr = JSONObject.toJSONString(a);
                    //转化为json对象
                    list.add(JSONObject.parseObject(jsonStr));
                }
            }
            if(list.size()>0){
                results = TimedTask.getHttpResponseJson(url,list);
            }else {
                log.info("工程-人员机构：数据不全，符合条件的数据为0条");
                return ;
            }

            if(results.getCode().equals(200)){
                log.info("工程-付款信息人员推送成功",results);
                UpdateWrapper<DwdGcFkxxJiande> wrapper = new UpdateWrapper<>();
                wrapper.like("uploadtime",time).set("bx","1");
                int b = dwdGcFkxxJiandeMapper.update(null,wrapper);
            }else {
                log.info("工程-付款信息人员推送失败",results);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
